﻿using Mono.Cecil;
using Mono.Cecil.Rocks;

namespace MethodCaching.Weaving.Extensions
{
    /// <summary>
    /// Extends the <see cref="TypeReference"/> class.
    /// </summary>
    public static class TypeReferenceExtensionss
    {
       
        public static MethodReference MakeHostInstanceGeneric(this MethodReference
self, params TypeReference[] arguments)
        {
            var reference = new MethodReference(self.Name, self.ReturnType,
      self.DeclaringType.MakeGenericInstanceType(arguments))
            {
                HasThis = self.HasThis,
                ExplicitThis = self.ExplicitThis,
                CallingConvention = self.CallingConvention
            };

            foreach (var parameter in self.Parameters)
                reference.Parameters.Add(new
      ParameterDefinition(parameter.ParameterType));

            foreach (var generic_parameter in self.GenericParameters)
                reference.GenericParameters.Add(new
      GenericParameter(generic_parameter.Name, reference));

            return reference;

        } 

    }


    
}
